package org.internna.ossmoney.mvc;

import java.util.List;
import java.util.TreeSet;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Executors;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.ScheduledExecutorService;
import org.internna.ossmoney.Launcher;
import org.internna.ossmoney.model.Category;
import org.internna.ossmoney.model.Subcategory;
import org.internna.ossmoney.services.AdminService;
import org.internna.ossmoney.model.security.UserDetails;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/financial/administration")
public class AdminController {

	@Autowired private AdminService service;

    @RequestMapping("/createInstitution")
    public String institution() {
        return "administration/createInstitution";
    }

    @RequestMapping(value = "/createInstitution", method = RequestMethod.POST)
    public String institution(String name, String web, String icon) {
    	service.createInstitution(name, web, icon);
        return "redirect:/financial/accounts";
    }

    @RequestMapping("/createPayee")
    public String payee() {
        return "administration/createPayee";
    }

    @RequestMapping(value = "/createPayee", method = RequestMethod.POST)
    public String payee(String name) {
    	service.createPayee(name);
        return "redirect:/financial/accounts";
    }

    @RequestMapping("/backup")
    public @ResponseBody byte[] backup(HttpServletResponse response) throws IOException {
    	UserDetails user = UserDetails.findCurrentUser();
    	byte[] data = service.backup(user);
    	response.setHeader("Content-Disposition", "attachment; filename=\"ossmoney-" + user.getUsername() + ".backup.zip\"");
    	return data;
    }

    @RequestMapping("/createSubcategory")
    public String subcategory(ModelMap modelMap) {
    	List<Category> categories = Category.findAllCategorys();
    	modelMap.addAttribute("categories", new TreeSet<Category>(categories.subList(2, categories.size())));
        return "administration/createSubcategory";
    }

    @RequestMapping(value = "/createSubcategory", method = RequestMethod.POST)
    public String subcategory(Long id, String name, ModelMap modelMap) {
    	UserDetails user = UserDetails.findCurrentUser();
    	Category category = Category.findCategory(id);
    	if (category != null) {
    		Subcategory.createInstance(name, category, user);
    	}
        return subcategory(modelMap);
    }

    @RequestMapping("/shutdown")
    public String shutdown() throws Exception {
    	ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    	scheduler.schedule(new Runnable() {
    		@Override public void run() {
    			Launcher.stop();
    		}
    	}, 1, TimeUnit.SECONDS);
    	return "administration/shutdown";
    }

}
